import { apiClient } from '@/lib/api-client';
import type { ApiResponse } from '@/lib/types/api';

// 健康记录类型
interface HealthRecord {
  _id: string;
  userId: string;
  recordType: "weight" | "bloodPressure" | "heartRate" | "sleepQuality";
  timestamp: Date;
  details: {
    weight?: {
      value: number;
      unit: "kg" | "lb";
    };
    bloodPressure?: {
      systolic: number;
      diastolic: number;
      pulse?: number;
    };
    heartRate?: {
      current: number;
      max?: number;
      min?: number;
      avg?: number;
    };
    sleepQuality?: {
      totalSleepDuration: number;
      sleepStartTime?: Date;
      wakeUpTime?: Date;
    };
  };
  remark?: string;
  source: "manual" | "device";
  deviceId?: string;
}

// 创建记录请求参数
interface CreateHealthRecordRequest {
  recordType: HealthRecord['recordType'];
  details: HealthRecord['details'];
  remark?: string;
  source: HealthRecord['source'];
  deviceId?: string;
}

// 获取记录列表的查询参数
interface GetRecordsOptions {
  recordType?: HealthRecord['recordType'];
  startDate?: string;
  endDate?: string;
  limit?: number;
  skip?: number;
}

// 统计数据接口
interface HealthStats {
  records: HealthRecord[];
  total: number;
  latestRecords: {
    weight?: HealthRecord;
    bloodPressure?: HealthRecord;
    heartRate?: HealthRecord;
    sleepQuality?: HealthRecord;
  };
}

export class HealthService {
  // 获取健康记录列表
  static async getRecords(options?: GetRecordsOptions): Promise<ApiResponse<HealthStats>> {
    const queryParams = new URLSearchParams();
    if (options) {
      if (options.recordType) queryParams.append('recordType', options.recordType);
      if (options.startDate) queryParams.append('startDate', options.startDate);
      if (options.endDate) queryParams.append('endDate', options.endDate);
      if (options.limit) queryParams.append('limit', options.limit.toString());
      if (options.skip) queryParams.append('skip', options.skip.toString());
    }

    return await apiClient<null, HealthStats>(`/health/records?${queryParams.toString()}`, {
      method: 'GET',
    });
  }

  // 创建健康记录
  static async createRecord(data: CreateHealthRecordRequest): Promise<ApiResponse<HealthRecord>> {
    return await apiClient<CreateHealthRecordRequest, HealthRecord>('/health/records', {
      method: 'POST',
      body: data,
    });
  }

  // 获取单个健康记录
  static async getRecord(recordId: string): Promise<ApiResponse<HealthRecord>> {
    return await apiClient<null, HealthRecord>(`/health/records/${recordId}`, {
      method: 'GET',
    });
  }

  // 更新健康记录
  static async updateRecord(recordId: string, data: Partial<CreateHealthRecordRequest>): Promise<ApiResponse<HealthRecord>> {
    return await apiClient<Partial<CreateHealthRecordRequest>, HealthRecord>(`/health/records/${recordId}`, {
      method: 'PUT',
      body: data,
    });
  }

  // 删除健康记录
  static async deleteRecord(recordId: string): Promise<ApiResponse<void>> {
    return await apiClient<null, void>(`/health/records/${recordId}`, {
      method: 'DELETE',
    });
  }

  // 获取健康趋势数据
  static async getHealthTrends(recordType: HealthRecord['recordType'], startDate: Date, endDate: Date): Promise<ApiResponse<any>> {
    const queryParams = new URLSearchParams({
      recordType,
      startDate: startDate.toISOString(),
      endDate: endDate.toISOString(),
    });

    return await apiClient<null, any>(`/health/stats/trends?${queryParams.toString()}`, {
      method: 'GET',
    });
  }
}

// 导出类型
export type { 
  HealthRecord, 
  CreateHealthRecordRequest, 
  GetRecordsOptions, 
  HealthStats 
};
